##############################################################################
# Filename: Setup.R
# Purpose: Set up variables and dataframes for subsequent analyses
##############################################################################

### Load packages 
library(dplyr)
library(tidyr)
library(estimatr)
library(stargazer)
library(starpolishr)
library(ggplot2)
library(ggeffects)
library(irr)


### Load data
load("replication_data.RData")


### Recode variables

## Political affiliation 
# Pro-BJP
data_full$ProBJP = ifelse(data_full$PartyMost=="BJP" & data_full$PartyLevel=="Extremely high" |
                    data_full$PartyMost=="BJP" & data_full$PartyLevel=="High" | 
                    data_full$PartyMost=="BJP" & data_full$PartyLevel=="Medium",
                  1,0)
data_full$ProBJP = as.factor(data_full$ProBJP)

# Alternate measures of political affiliation
data_full$ProModi = ifelse(data_full$PMSupp=="Extremely happy"|data_full$PMSupp=="Happy",1,0)
data_full$ExtremeBJP = ifelse(data_full$PartyMost=="BJP"&data_full$PartyLevel=="Extremely high", 1, 0)
data_full$PreferBJP = ifelse(data_full$PartyMost=="BJP", 1, 0)

## Demographics
# Region
data_full = data_full %>% mutate(Zone = case_when(
  StateLive %in% c("Chandigarh", "Delhi", "Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Punjab", "Rajasthan") ~ "North",
  StateLive %in% c("Assam", "Arunachal Pradesh", "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Sikkim", "Tripura") ~ "Northeast",
  StateLive %in% c("Chhattisgarh", "Madhya Pradesh", "Uttarakhand", "Uttar Pradesh") ~ "Central",
  StateLive %in% c("Bihar", "Jharkhand", "Odisha", "West Bengal") ~ "East",
  StateLive %in% c("Dadra and Nagar Haveli", "Daman and Diu", "Goa", "Gujarat", "Maharashtra") ~ "West",
  StateLive %in% c("Andhra Pradesh", "Karnataka", "Kerala", "Puducherry", "Tamil Nadu", "Telangana", "Andaman and Nicobar Islands", "Lakshadweep") ~ "South"
))
data_full$Zone = as.factor(data_full$Zone)
    data_full$Zone_Central = ifelse(data_full$Zone=="Central", 1, 0)
    data_full$Zone_East = ifelse(data_full$Zone=="East", 1, 0)
    data_full$Zone_North = ifelse(data_full$Zone=="North", 1, 0)
    data_full$Zone_Northeast = ifelse(data_full$Zone=="Northeast", 1, 0)
    data_full$Zone_South = ifelse(data_full$Zone=="South", 1, 0)
    data_full$Zone_West = ifelse(data_full$Zone=="West", 1, 0)

# Religion
data_full$Hindu = ifelse(data_full$Religion=="Hinduism",1,0)
data_full$Muslim = ifelse(data_full$Religion=="Islam",1,0)
data_full$Christian = ifelse(data_full$Religion=="Christianity",1,0)
data_full$Religion_non3 = ifelse(data_full$Hindu==0 & data_full$Muslim==0 & data_full$Christian==0, 1, 0)

# Caste
data_full$Caste_Brahmin = ifelse(data_full$Caste=="Brahmin",1,0)
data_full$Caste_Forward = ifelse(data_full$Caste=="Forward",1,0)
data_full$Caste_SCST = ifelse(data_full$Caste=="SCST",1,0)
data_full$Caste_OBC = ifelse(data_full$Caste=="OBC",1,0)
data_full$Caste_NotApp = ifelse(data_full$Caste=="NotApp",1,0)

## Treatment condition 
# Article alignment
data_full$Article_Alignment = ifelse(data_full$Article == "DP" & data_full$ProBJP == 1, "pro",
                              ifelse(data_full$Article == "DP" & data_full$ProBJP == 0, "counter",
                                    ifelse(data_full$Article == "DN" & data_full$ProBJP == 1, "counter",
                                           ifelse(data_full$Article == "DN" & data_full$ProBJP == 0, "pro", "none"))))
data_full$Article_Alignment = factor(data_full$Article_Alignment,levels=c("none","counter","pro"))


### Set up dependent variables for written article evaluation 
# Label a comment as biased if two or all of the three coders marked it as biased
data_full$CommentBiasSum = data_full$Coder1_Biased + data_full$Coder2_Biased + data_full$Coder3_Biased
data_full$CommentBiased = ifelse(data_full$CommentBiasSum==3 | data_full$CommentBiasSum==2, 1, 0)

# Label feelings about the article based on the modal label
data_full$CommentFeeling = ifelse(data_full$Coder1_Feeling == data_full$Coder2_Feeling | data_full$Coder1_Feeling == data_full$Coder3_Feeling, data_full$Coder1_Feeling, 
                                  ifelse(data_full$Coder2_Feeling == data_full$Coder3_Feeling, data_full$Coder2_Feeling, "??"))
data_full$CommentFeeling = as.factor(data_full$CommentFeeling)


### Subset of data with only those in treatment groups who completed the article evaluation exercise
data_eval = data_full[which(data_full$CompletionStatus=="Finished" | data_full$CompletionStatus=="PolicyopsDone" | data_full$CompletionStatus=="NewsConsArea"),]
data_eval = data_eval[which(data_eval$Article!="pl"),]
data_eval = droplevels(data_eval)


### Subset of data with those who completed issue opinios section
data_op = data_full[which(data_full$CompletionStatus=="Finished" | data_full$CompletionStatus=="PolicyopsDone"),]
data_op = droplevels(data_op)